//
//  ClockViewController.m
//  CoreAnimationDemo
//
//  Created by 王亮 on 2017/8/7.
//  Copyright © 2017年 王亮. All rights reserved.
//

#import "ClockViewController.h"
#import <CoreGraphics/CoreGraphics.h>

@interface ClockViewController ()

@property (nonatomic, strong)UIView *layerView;
@property (nonatomic, strong)CALayer *clockBgLayer;
@property (nonatomic, strong)CALayer *hourLayer;
@property (nonatomic, strong)CALayer *minLayer;
@property (nonatomic, strong)CALayer *secLayer;

@property (nonatomic, strong)NSTimer *timer;

@end

@implementation ClockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.layerView = [[UIView alloc] init];
    self.layerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.layerView];
    
    UIImage *image = [UIImage imageNamed:@"papapa"];
    self.clockBgLayer = [CALayer layer];
    self.clockBgLayer.frame = CGRectMake(0, 0, 200, 200);
    [self.layerView.layer addSublayer:self.clockBgLayer];
    [self addSpriteImage:image contentsRect:CGRectMake(0.05, 0, 0.43, 1.0) toLayer:self.clockBgLayer];
    
    CGPoint tmpCenter = CGPointMake(100, 100);
    self.hourLayer = [CALayer layer];
    self.hourLayer.frame = CGRectMake(0, 0, 40, 120);
    self.hourLayer.position = tmpCenter;
    [self.layerView.layer addSublayer:self.hourLayer];
    self.hourLayer.backgroundColor = [UIColor clearColor].CGColor;
    [self addSpriteImage:image contentsRect:CGRectMake(0.6, 0, 0.03, 1.0) toLayer:self.hourLayer];
    
    self.minLayer = [CALayer layer];
    self.minLayer.frame = CGRectMake(0, 0, 40, 120);
    [self.layerView.layer addSublayer:self.minLayer];
    self.minLayer.position = tmpCenter;
    self.minLayer.backgroundColor = [UIColor clearColor].CGColor;
    [self addSpriteImage:image contentsRect:CGRectMake(0.75, 0, 0.05, 1.0) toLayer:self.minLayer];
    
    self.secLayer = [CALayer layer];
    self.secLayer.frame = CGRectMake(0, 0, 40, 120);
    [self.layerView.layer addSublayer:self.secLayer];
    self.secLayer.position = tmpCenter;
    self.secLayer.backgroundColor = [UIColor clearColor].CGColor;
    [self addSpriteImage:image contentsRect:CGRectMake(0.92, 0, 0.05, 1.0) toLayer:self.secLayer];
    
    self.hourLayer.anchorPoint = CGPointMake(0.5, 0.9);
    self.minLayer.anchorPoint = CGPointMake(0.5, 0.9);
    self.secLayer.anchorPoint = CGPointMake(0.5, 0.9);
    
    __weak __typeof(self)weakSelf = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        //convert time to hours, minutes and seconds
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
        CGFloat hoursAngle = (components.hour / 12.0) * M_PI * 2.0;
        //calculate hour hand angle //calculate minute hand angle
        CGFloat minsAngle = (components.minute / 60.0) * M_PI * 2.0;
        //calculate second hand angle
        CGFloat secsAngle = (components.second / 60.0) * M_PI * 2.0;
        //rotate hands
        weakSelf.hourLayer.transform = CATransform3DMakeRotation(hoursAngle, 0, 0, 1);
        weakSelf.minLayer.transform = CATransform3DMakeRotation(minsAngle, 0, 0, 1);
        weakSelf.secLayer.transform = CATransform3DMakeRotation(secsAngle, 0, 0, 1);

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.layerView.frame = CGRectMake(0, 0, 200, 200);
    self.layerView.center = self.view.center;
}

- (void)addSpriteImage:(UIImage *)image contentsRect:(CGRect)contentsRect toLayer:(CALayer *)layer {
    layer.contents = (__bridge id _Nullable)(image.CGImage);
    
    layer.contentsRect = contentsRect;
    
    layer.contentsGravity = kCAGravityResizeAspect;
}

@end
